﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;
using Microsoft.Web.WebSockets;
using TheServer.Models;

namespace TheServer
{

    public class ChatSocket : WebSocketHandler
    {
        private string user;
        private JavaScriptSerializer serializer = new JavaScriptSerializer();
        private static WebSocketCollection chatapp = new WebSocketCollection();

        public ChatSocket()
        { 
        }

        public ChatSocket(string username)
        {
            user = username;
        }

        public override void OnMessage(string message)
        {
            var msg = serializer.Deserialize<MessageDTO>(message);
            WebSocketHandler channel = null;
            //switch (msg.Type)
            //{
            //    case MessageType.Broadcast:
            //        chatapp.Broadcast(serializer.Serialize(new
            //        {
            //            Type = msg.Type,
            //            UserName = user,
            //            Value = msg.Value,
            //            From = user
            //        }));

            //        break;
            //    case MessageType.Send:
            channel = chatapp.FirstOrDefault(n => ((ChatSocket)n).user == msg.TaiKhoanNhan);
            if (channel != null)
                channel.Send(message);
            else
                MessageDAO.PostMessage(msg);
               
        }
        public override void OnClose()
        {
            AccountDTO taiKhoanthoat=new AccountDTO();
            taiKhoanthoat.TaiKhoan=this.user;
            AccountDAO.LogOut(taiKhoanthoat);

            MessageDTO notice = new MessageDTO();
            notice.Type = 1;
            List<AccountDTO> friend = FriendDAO.GetStatusFriendsReverse(this.user);
            if (friend != null)
            {
                for (int i = 0; i < friend.Count; i++)
                {
                    var userFr = chatapp.FirstOrDefault(n => ((ChatSocket)n).user == friend[i].TaiKhoan);
                    if (userFr != null)
                        userFr.Send(serializer.Serialize(notice));
                }
            }

            chatapp.Remove(this);
        }
        public override void OnOpen()
        {
            MessageDTO notice=new MessageDTO();
            notice.Type=1;
            List<AccountDTO> friend = FriendDAO.GetStatusFriendsReverse(this.user);
            if (friend != null)
            {
                for (int i = 0; i < friend.Count; i++)
                {
                    var userFr = chatapp.FirstOrDefault(n => ((ChatSocket)n).user == friend[i].TaiKhoan);
                    if (userFr != null)
                        userFr.Send(serializer.Serialize(notice));
                }
            }

            List<MessageDTO> offMsg = MessageDAO.GetMessage(this.user);
            

            if (offMsg != null)
            {
                MessageDTO offMess = new MessageDTO();
                offMess.Type = 3;
                offMess.NoiDung = serializer.Serialize(offMsg);
                this.Send(serializer.Serialize(offMess));
            }
            chatapp.Add(this);
        }

    }
}
